package com.szgr.baoyin.utils;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by yang on 15/10/18.
 */
public class DateUtils {
    private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

    private DateUtils(){}

    /**
     * 获取每个月1号是星期几
     * 1 代表 星期天   依此类推
     * @param month  yyyy-MM  入参格式
     * @return
     */
    public static int getDayOfWeek(String month){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
        Date parse = null;
        try {
            parse = sdf.parse(month + "-01");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(parse);
        int i = calendar.get(Calendar.DAY_OF_WEEK);
        return i;
    }

    /**
     *
     * @return
     */
    public static int getMonthDays(String month){
        String[] temp = month.split("-");
        int y = Integer.parseInt(temp[0]);
        int m = Integer.parseInt(temp[1]);

        switch (m){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if((y % 4 == 0 && y % 100 != 0) || (y % 4 == 0 && y % 400 == 0)){
                    return 29;
                }else{
                    return 28;
                }
        }

        return 0;
    }

    public static int compare_date(String date1, String date2) {
        SimpleDateFormat df = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
        try {
            Date dt1 = df.parse(date1);
            Date dt2 = df.parse(date2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (ParseException e) {
            Log.i("DateUtils", e.getMessage());
        }
        return 0;
    }

    public static String paseDate(long time){
        SimpleDateFormat df = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
        return df.format(time * 1000);
    }

    public static String paseDate1(long time){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(time * 1000);
    }
}
